import requests
from ..shippypro import api_key, headers, shippypro_url
import json
from .base_api import BaseAPI

key = api_key
headers = headers
url = shippypro_url
from odoo import _, api, models
from odoo.modules.registry import Registry
from odoo.tools.config import config
class OrdersAPI(BaseAPI):
    """ 订单接口 """

    @classmethod
    def delete_pending_order(cls, transaction_id: str, order_id=False) -> dict:
        """
        将未发货的订单作废
        :param transaction_id:运输追踪号
        :param order_id: 订单号
        """
        APIOrdersID = super().get_config("APIOrdersID")
        return super().post("DeletePendingOrder", {
                "APIOrdersID": APIOrdersID,
                "TransactionID": transaction_id
            })
        # if order_id:
        #     values['Params'].update({"APIOrdersID": order_id})
        # print(values)
        # data = json.dumps(values)
        # response = requests.post(url, data=data, headers=headers)
        # response_dict = json.loads(response.text)
        # print(response_dict)
        # return response_dict

    @classmethod
    def get_order_tracking(cls, tracking_number: str) -> dict:
        """
        获取订单运输信息
        :param tracking_number:订单的追踪号
        :return {"Carrier": "UPS",
                 "CarrierLogo": "",
                 "Completed": true,
                 "EstimatedDeliveryDate": "1489659960",
                 "StatusCode": 6,
                 "Details": [{"date": "1489659960",
                              "message": "DELIVERED",
                              "city": "MOTRIL",
                              "StatusCode": 6},
                             {"date": "1489625040",
                              "message": "OUT FOR DELIVERY",
                              "city": "GUARROMAN",
                              "StatusCode": 3},]}
        """
        values = {
            "Method": "GetTracking",
            "Params": {
                "Code": tracking_number
            }
        }
        print(values)
        data = json.dumps(values)
        response = requests.post(url, data=data, headers=headers)
        response_dict = json.loads(response.text)
        print(response_dict)
        return response_dict
